package cs575.update;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import cs575.common.Pair;

public class StatDialog extends JDialog implements ActionListener {
	private static final long serialVersionUID = 3007193952599328675L;

	private JTextField statName;
	private JTextField statValue;

	private Pair<String, Integer> stat;

	public StatDialog(JFrame parent) {
		super(parent);
		setModal(true);
		setTitle("Add Statistic");

		SpringLayout layout = new SpringLayout();
		JPanel content = new JPanel(layout);

		JLabel statLabel = new JLabel("Add Player Statistic");
		layout.putConstraint(SpringLayout.NORTH, statLabel, 10,
				SpringLayout.NORTH, content);
		layout.putConstraint(SpringLayout.WEST, statLabel, 10,
				SpringLayout.WEST, content);
		content.add(statLabel);

		statName = new JTextField();
		layout.putConstraint(SpringLayout.NORTH, statName, 10,
				SpringLayout.SOUTH, statLabel);
		layout.putConstraint(SpringLayout.EAST, statName, -10,
				SpringLayout.EAST, content);
		content.add(statName);

		JLabel snLabel = new JLabel("Statistic Name:");
		layout.putConstraint(SpringLayout.VERTICAL_CENTER, snLabel, 0,
				SpringLayout.VERTICAL_CENTER, statName);
		layout.putConstraint(SpringLayout.WEST, snLabel, 10, SpringLayout.WEST,
				content);
		content.add(snLabel);

		statValue = new JTextField();
		layout.putConstraint(SpringLayout.NORTH, statValue, 10,
				SpringLayout.SOUTH, statName);
		layout.putConstraint(SpringLayout.EAST, statValue, -10,
				SpringLayout.EAST, content);
		content.add(statValue);

		JLabel svLabel = new JLabel("Statistic Value:");
		layout.putConstraint(SpringLayout.VERTICAL_CENTER, svLabel, 0,
				SpringLayout.VERTICAL_CENTER, statValue);
		layout.putConstraint(SpringLayout.WEST, svLabel, 10, SpringLayout.WEST,
				content);
		content.add(svLabel);

		layout.putConstraint(SpringLayout.WEST, statName, 10,
				SpringLayout.EAST, svLabel);
		layout.putConstraint(SpringLayout.WEST, statValue, 10,
				SpringLayout.EAST, svLabel);

		JButton add = new JButton("Add");
		add.setActionCommand("ADD");
		add.addActionListener(this);
		layout.putConstraint(SpringLayout.NORTH, add, 10, SpringLayout.SOUTH,
				statValue);
		layout.putConstraint(SpringLayout.WEST, add, 10, SpringLayout.WEST,
				content);
		content.add(add);

		JButton cancel = new JButton("Cancel");
		cancel.setActionCommand("CANCEL");
		cancel.addActionListener(this);
		layout.putConstraint(SpringLayout.NORTH, cancel, 10,
				SpringLayout.SOUTH, statValue);
		layout.putConstraint(SpringLayout.WEST, cancel, 10, SpringLayout.EAST,
				add);
		content.add(cancel);

		setSize(300, 170);
		setLocationRelativeTo(parent);
		setContentPane(content);
	}

	public Pair<String, Integer> getStat() {
		return stat;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		String command = arg0.getActionCommand();

		if (command.equals("ADD")) {
			stat = new Pair<String, Integer>(statName.getText(),
					Integer.parseInt(statValue.getText()));
			setVisible(false);
		} else if (command.equals("CANCEL")) {
			stat = null;
			setVisible(false);
		}

	}
}
